package com.test.study.protocol;

import com.test.study.util.SerializationUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @ClassName GpRespEncoder
 * @Description TODO
 * @Author USER
 * @Date 2019/7/15 17:17
 * @Company
 **/
public class GpRespEncoder extends MessageToByteEncoder {

    private Class<?> genericClass;


    public GpRespEncoder(Class<?> genericClass){
        this.genericClass = genericClass;
    }
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {

        if(msg.getClass() == genericClass){
            byte[] resp = SerializationUtil.serialize(msg);
            out.writeInt(resp.length);
            out.writeBytes(resp);

        }
    }
}
